<?php 
	
	define('DOMAIN_NAME', 'http://widocom.com/projects/nursenews/pc/');
	define('DB_HOSTNAME', 'localhost');
	define('DB_USERNAME', 'root');
	define('DB_PASSWORD', '');
	define('DB_DATABASENAME', 'nurse');
	
	define('COOKIE_FAV_NAME', 'nurse_cookie_fav');
	define('COOKIE_SITE_ADD', 'nurse_cookie_site');
	define('COOKIE_SITE_ADD_TMP', 'nurse_cookie_site_tmp');

	function renderSites()
	{
		$siteCookies = isset($_COOKIE[COOKIE_SITE_ADD]) ? $_COOKIE[COOKIE_SITE_ADD] : '1,2,3,4,5,6,8,9,10,12,13,15,16' ;
		$html = '';
		$html .= '<div class="row">';
		$arraySites = array('1', '2', '3', '4', '5', '6', '8', '9', '10', '12', '13', '15', 16);
		foreach ($arraySites as $value) {
			$selected = '';
			if(in_array($value, explode(',', $siteCookies))){
				$selected = ' selected';
			}
			//$html .= '<div class="box" style="height:120px;margin-bottom:1px;width: 11%;"><div class="content"><a class="change_site site'.$value.$selected.'" title="'.(int)$value.'"></a></div></div>';
			
				$html .= '<div class="col-sm-2 col-md-2" style="height:120px;padding:0;margin:0;">';
					$html .= '<a class="change_site site'.$value.$selected.'" title="'.(int)$value.'"></a>';
				$html .= '</div>';
			
		}
		$html .= '</div>';
		$html .= '<div class="clearfix"></div>';
		
		$html .= '<div class="box clearfix" style="height:120px;margin-bottom:1px;width: 150px;">
							<div class="content">
								<form action="'.DOMAIN_NAME.'index.php" method="GET" class="from-inline" style="float:left;">
									<input type="hidden" value="s" name="s" />
									<button type="submit" class="btn btn-info">保存</button>
								</form>
								
								<form action="'.DOMAIN_NAME.'index.php" method="get" class="from-inline"  style="float:left;">
									<input type="hidden" value="c" name="s" />
									<button type="submit" class="btn btn-warning " style="margin-left:10px;">取り消し</button>
								</form>
							</div>
							</div>';
		
		return $html;
	}
	
	function renderBox($items, $currentCookie = array(), $isChanged = false)
	{
		$result = '';
		$index = 0;
		if($isChanged){
				$arrItemIds = $currentCookie;
		} else {
				$cookie = isset($_COOKIE[COOKIE_FAV_NAME]) ? $_COOKIE[COOKIE_FAV_NAME] : '';
				$arrItemIds = explode(',', $cookie);
		}

		foreach ($items as $item) {
			
			if($index == 2){
				$result .= '<div class="box">
							<div class="appli_store">
	            <span style="clear: both; display:block; height:50px;"></span>
	            <a href="http://itunes.apple.com/jp/app/123456" target="_blank">
	                <img src="images/apple_store.png">
	                for iPhone/iPad
	            </a>
	            <a href="https://play.google.com/store/apps/details?id=com.kingcut.webmobile" target="_blank">
	                <img src="images/google_play.png">
	                for Android
	            </a>
	        </div>
	        </div>
	        <span style="clear: both; display:block"></span>';
				
				$index++;
			}		

			if(in_array($item['id'], $arrItemIds)){
				$item['favorite'] = 'unfavorite';
			} else {
				$item['favorite'] = 'favorite';
			}
			$images = json_decode($item['images']);
			$lead = $item['lead'];
			if (is_array($images) && count($images) > 0) {
				$img = $images[0];
				$lead = '<img src="images/'.$img.'" alt="" style="max-width: 100%" />';
			}
			

			$result .= '<div class="box">
                        <div class="source_title" style="border-top-color:'.$item['color'].'">
							<span class="date"><img src="images/like-button.png" alt=""/>&nbsp;<span style="color:red">'.$item['fb_like'].'</span>&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/twitter-button.png" alt=""/>&nbsp;<span style="color:red">'.$item['tw_like'].'</span> </span>
                        </div>
                        <div class="content">
                            <p class="title">
                                <a href="'.$item['link'].'" target="_blank">'.$item['title'].'</a>
                            </p>
							<p class="source"><a>'.$item['name'].'</a></p>
                            <p class="lead">'.$lead.'</p>
                            <div class="bottom">
                                <a class="category '.$item['favorite'].'" href="javascript:void(0)" title="'.$item['id'].'"></a>
                            </div>
                        </div>
                    </div>';
			if($index > 0 && $index % 3 == 2){
				$result .= '<span style="clear: both; display:block"></span>';
			}	
		
			$index++;
		}
		
		if(count($items) < 3 && count($items) > 0){
				$result .= '<div class="box">
							<div class="appli_store">
	            <span style="clear: both; display:block; height:50px;"></span>
	            <a href="http://itunes.apple.com/jp/app/123456" target="_blank">
	                <img src="images/apple_store.png">
	                for iPhone/iPad
	            </a>
	            <a href="https://play.google.com/store/apps/details?id=com.kingcut.webmobile" target="_blank">
	                <img src="images/google_play.png">
	                for Android
	            </a>
	        </div>
	        </div>
	        <span style="clear: both; display:block"></span>';
			}
		
		return $result;
	}
	
	function getConnection()
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
		$db = mysql_select_db(DB_DATABASENAME, $conn);
		mysql_query("SET NAMES 'utf8'", $conn);
		if (!$db) {
		    die('Could not connect: ' . mysql_error());
		}
		
		return $conn;
	}
	
	function closeConnection($conn)
	{
		mysql_close($conn);
	}
	
	function fetchData($query)
	{
		$data = array();	
	
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) {
	    $data[] = $row;
		}
		mysql_free_result($result);
		
		return $data;
	}

?>